/**
 * IndexTuningDemo.java 1.0 2007-6-9 下午09:29:53,创建 by yangfan
 */


package lia.ch1;


import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.in4j.core.common.TimeLogger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 *
 * @author yangfan
 * @version 1.0 2007-6-9 下午09:29:53,创建
 */
public class IndexTuningDemo
{

	@Before
	public void setUp() throws Exception
	{
	}

	@After
	public void tearDown() throws Exception
	{
	}

	@Test
	public void test1() throws Exception
	{
		Directory indexDir = getIndexDir();

		IndexWriter writer = new IndexWriter(indexDir,getAnalyzer(),true);
//		writer.addIndexes((Directory[])null);
//		writer.setMergeFactor(100);
//		writer.setMaxMergeDocs(1000);
//		writer.setMaxBufferedDocs(100);
		writer.setUseCompoundFile(false);
		String propTmpDir = "java.io.tmpdir";
		String propLockDir = "org.apache.lucene.lockDir";
		System.out.println(propTmpDir + "==>" + System.getProperty(propTmpDir));
		System.out.println(propLockDir + "==>" + System.getProperty(propLockDir));
		writer.setInfoStream(System.out);
		int count = 3;
		TimeLogger tl = new TimeLogger();

		for(int i = 0; i < count; i++)
		{
			Document doc = new Document();
			doc.add(new Field("id",String.valueOf(i),Store.YES,Index.UN_TOKENIZED));
			doc.add(new Field("fieldname","Voodoo",Store.NO,Index.TOKENIZED));
			writer.addDocument(doc);
		}

		writer.optimize();
		writer.close();

		System.out.println("共索引了" + count + "个文档,耗时:" + tl.getElapsedTime() + "ms.");
	}

	private Analyzer getAnalyzer()
	{
		return new StandardAnalyzer();
	}

	private Directory getIndexDir() throws IOException
	{
		String path = System.getProperty("java.io.tmpdir","tmp") + File.separator + "index-dir";
//		File path = File.createTempFile("index-dir", "");
		Directory dir = FSDirectory.getDirectory(path, true);
		return dir;
	}

}
